#region "DatabaseFactory"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014-07-16 14:26:38
 * 
 * ####     Namespace : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data
 * 
 * ####     Type Name : DatabaseFactory
 * 
 * ####     Full Name : Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data.DatabaseFactory
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System;
using System.Configuration;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Configuration;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data.Common;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data.Configuration;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary.Runtime;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data
{
    /// <summary>
    /// <para>Masterduner.SourceStudio.Practices.DevelopmentLibrary.Data.DatabaseFactory</para>
    /// <para>
    /// 提供了创建<see cref="Database"/>对象实例的方法。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public static class DatabaseFactory
    {
        #region CreateDatabase
        /// <summary>
        /// 根据"dbConnectionsDeclaration"配置节创建指定名称的<see cref="Database"/>对象实例。
        /// </summary>
        /// <param name="name">配置在dbConnectionsDeclaration节中的数据库连接名称。</param>
        /// <param name="platform"><see cref="RunPlatformID"/>中的一个值。</param>
        /// <returns><see cref="Database"/>对象实例。</returns>
        /// <remarks>
        /// <para>
        /// 当<paramref name="platform"/>等于<see cref="RunPlatformID"/>.Unknown时，将抛出<see cref="DatabaseExceptionWrapper"/>异常。
        /// </para>
        /// <para>
        /// 如果未发现数据库配置，将抛出<see cref="ConfigurationExceptionWrapper"/>异常。
        /// </para>
        /// </remarks>
        static public Database CreateDatabase(string name, RunPlatformID platform)
        {
            if (platform.Equals(RunPlatformID.Unknown)) throw new DatabaseExceptionWrapper();
            return CreateDatabaseUseConfiguration(name, platform);
        }

        /// <summary>
        /// 根据"dbConnectionsDeclaration"配置节创建指定名称的<see cref="Database"/>对象实例。
        /// </summary>
        /// <param name="name">配置在dbConnectionsDeclaration节中的数据库连接名称。</param>
        /// <returns><see cref="Database"/>对象实例。</returns>
        /// <para>
        /// 如果未发现数据库配置，将抛出<see cref="ConfigurationExceptionWrapper"/>异常。
        /// </para>
        static public Database CreateDatabase(string name)
        {
            RunPlatformID platform = RunPlatformRegistration.Platform;
            if (platform.Equals(RunPlatformID.Unknown)) platform = RunPlatformRegistration.AutoDiscoverPlatform();
            return CreateDatabase(name, platform);
        }
        #endregion

        #region CreateDatabaseUseConfiguration
        /// <summary>
        /// 根据"dbConnectionsDeclaration"配置节创建指定名称的<see cref="Database"/>对象实例。
        /// </summary>
        /// <param name="name">配置在dbConnectionsDeclaration节中的数据库连接名称。</param>
        /// <param name="platform"><see cref="RunPlatformID"/>中的一个值。</param>
        /// <returns><see cref="Database"/>对象实例。</returns>
        /// <para>
        /// 如果未发现数据库配置，将抛出<see cref="ConfigurationExceptionWrapper"/>异常。
        /// </para>
        static private Database CreateDatabaseUseConfiguration(string name, RunPlatformID platform)
        {
            ConfigurationSectionGroup dataConfiguration = new DataConfigurationService(platform).GetConfiguration();
            if (dataConfiguration.IsNull()) throw new ConfigurationExceptionWrapper();
            DatabaseConnectionElement connectionConfig = dataConfiguration.Sections[DatabaseConnectionSection.SectionName].ForceTo<DatabaseConnectionSection>().DatabaseConnections[name];
            if (connectionConfig.IsNull()) throw new ConfigurationExceptionWrapper();
            DatabaseProviderElement providerConfig = dataConfiguration.Sections[DatabaseProviderSection.SectionName].ForceTo<DatabaseProviderSection>().DatabaseProviders[connectionConfig.DatabaseProviderName];
            if (connectionConfig.IsNull()) throw new ConfigurationExceptionWrapper();
            Type type = TypeExpando.GetType(providerConfig.Type);
            Database database = Creator.CreateInstance(type, connectionConfig.ConnectionString).ForceTo<Database>();
            database.ExceptionHandling = (DatabaseExceptionProcessMode)connectionConfig.ExceptionProcessMode;
            database.ExecuteTimeout = connectionConfig.CommandExecuteTimeout;
            return database;
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion